package com.xiaomo.gitee.utils;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class XMLUtils {

    public static void setMybatisConfigUrl(String url,File file) throws Exception {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new InputStreamReader(new FileInputStream(file),Charset.forName("GBK")));
        Element root = document.getRootElement();
        Element element = (Element) root.elements("environments").get(0);
        Element element1 = (Element) element.element("environment").element("dataSource").elements().get(1);
        element1.addAttribute("value",url);
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("GBK");
        XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
        writer.write(document);
        writer.close();
    }

}
